﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace idea.tools
{
    class DateSelectTool
    {
        private MonthCalendar monthCalendar;
        public delegate void _EXEC(string date);
        public _EXEC _exec;
        public DateSelectTool(Control parnet)
        {
            if (monthCalendar == null)
            {
                monthCalendar = new MonthCalendar();
                parnet.Controls.Add(monthCalendar);
            
                monthCalendar.Location = parnet.Location;
                monthCalendar.BringToFront();
                monthCalendar.Width = 100;
                monthCalendar.Height = 200;
                monthCalendar.Visible = false;

                parnet.Controls.Add(monthCalendar);
                monthCalendar.Visible = false;
                monthCalendar.DateSelected += OnDataSelected;
                monthCalendar.Focus();
                monthCalendar.BringToFront();
                monthCalendar.MouseLeave += monthCalendar_MouseLeave;
             //   monthCalendar.MouseLeave += new EventHandler(monthCalendar_MouseLeave);
                
            }

        }

        void monthCalendar_MouseLeave(object sender, EventArgs e)
        {
            hide();
        }

     
     

        
        public void show(_EXEC exec,Point location)
        {
            if (monthCalendar != null)
            {
                monthCalendar.Location = location;
                monthCalendar.Visible = true;
                _exec = exec;
            }
        }
        public void hide()
        {
            if (monthCalendar != null) 
                monthCalendar.Visible = false;
        }
        private void OnDataSelected(object sender, DateRangeEventArgs e)
        {
            hide();
            _exec(e.Start.ToShortDateString());
        }
    }
}
